﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using slava.dao;

namespace DAO
{
    public class ClientsRepository : IDao<Client>
    {
        skladEntities sklad;
        public ClientsRepository()
        {
            sklad = new skladEntities();
        }

        public IEnumerable<Client> GetAll()
        {
            return sklad.Clients;
        }

        public void Edit(Client entity)
        {
            var temp = sklad.Clients.Single(x => x.Id == entity.Id);
            sklad.Entry(temp).CurrentValues.SetValues(entity);
            sklad.SaveChanges(); 
        }

        public void Add(Client entity)
        {
            sklad.Clients.Add(entity);
            sklad.SaveChanges();
        }

        public void Delete(int id)
        {
            Client cl = sklad.Clients.FirstOrDefault(t => t.Id == id);
            sklad.Clients.Remove(cl);
            sklad.SaveChanges();
        }

        public Client GetById(int id)
        {
            return sklad.Clients.FirstOrDefault(t => t.Id == id);
        }
    }
}
